﻿using System;
using com.huigo.Framework.BLL;
using System.Collections.Generic;
using System.Web.Security;


namespace com.huigo.Framework.Service
{
    public partial class ServiceClass
    {
        //创建订单
        public string AddOrderInfo(long orderCustFid, long productId, string policyInfo)
        {
            if (orderCustFid > 0 && productId > 0)
            {
                try
                {
                    Response<Model.OrderInfo> el = new Response<com.huigo.Framework.Model.OrderInfo>();
                    long result = BLL.OrderInfo.InsertOrderInfo(orderCustFid, productId, policyInfo, Source);
                    if (result > 0)
                    {
                        Model.OrderInfo orderInfo = BLL.OrderInfo.getOrderNoByOrderId(result);
                        el.Status = "SUC";
                        el.Result = orderInfo;
                        return com.loto.Common.SerializationHelper.GetSerializationString(el);
                    }
                    else
                    {
                        return string.Empty;
                    }
                }
                catch (Exception ex)
                {
                    Log.Write(LogType.ERR, "AddOrderInfo(" + orderCustFid + ", " + productId + ") error:" + ex.Message);
                    return string.Empty;
                }
            }
            else
            {
                return string.Empty;
            }
        }

        //添加保险订单扩展表 --可能没用
        public string AddPolicy(long orderId, long custFid, string type, string holderName, string holderNo, string holderBirth, string holderGender, string insuredName, string insuredType, string insuredNo, string insuredBirth, string insuredGender, string fromPeriod, int count, decimal price)
        {
            try
            {
                Response<OneResult> el = new Response<OneResult>();
                long air = BLL.PolicyInfoAir.InsertPolicyInfoAir(orderId, custFid, holderName, type, holderNo, holderBirth, holderGender, insuredName, insuredType, insuredNo, insuredBirth, insuredGender, fromPeriod, count, price);
                el.Status = "SUC";
                el.Result = new OneResult(air);
                return com.loto.Common.SerializationHelper.GetSerializationString(el);
            }
            catch (Exception ex)
            {
                Log.Write(LogType.ERR, "AddOrderInfo(" + orderId + ", " + custFid + "," + type + ", " + holderName + "," + holderNo + "," + holderBirth + "," + holderGender + "," + insuredName + "," + insuredType + "," + insuredNo + "," + insuredBirth + "," + insuredGender + "," + fromPeriod + "," + count + "," + price + ") error:" + ex.Message);
                return string.Empty;
            }
        }

        //根据订单ID查询未支付订单信息
        public string GetUnpayOrderInfo(long custId, long orderId)
        {
            try
            {
                Response<Model.OrderInfo> el = new Response<Model.OrderInfo>();
                Model.OrderInfo orderInfo = BLL.OrderInfo.GetUnpayOrderInfoById(custId, orderId);
                if (orderInfo != null)
                {
                    el.Status = "SUC";
                    el.Result = orderInfo;
                    return com.loto.Common.SerializationHelper.GetSerializationString(el);
                }
                else
                {
                    return string.Empty;
                }
            }
            catch (Exception ex)
            {
                Log.Write(LogType.ERR, "GetUnpayOrderInfo(" + custId + "," + orderId + ") error:" + ex.Message);
                return string.Empty;
            }
        }

        //根据订单ID查询未支付发票订单信息
        public string GetUnpayInvoiceOrder(long custId, long orderId)
        {
            try
            {
                Response<Model.InvoiceOrder> el = new Response<Model.InvoiceOrder>();
                Model.InvoiceOrder orderInfo = BLL.InvoiceOrder.GetUnpayOrderInfoById(custId, orderId);
                if (orderInfo != null)
                {
                    el.Status = "SUC";
                    el.Result = orderInfo;
                    return com.loto.Common.SerializationHelper.GetSerializationString(el);
                }
                else
                {
                    return string.Empty;
                }
            }
            catch (Exception ex)
            {
                Log.Write(LogType.ERR, "GetUnpayInvoiceOrder(" + orderId + ") error:" + ex.Message);
                return string.Empty;
            }
        }
        

        //根据外部交易号查询支付发起信息
        public string GetFeeInitByOrderId(string orderId)
        {
            try
            {
                Response<Model.CustFeeInit> el = new Response<Model.CustFeeInit>();
                Model.CustFeeInit feeInfo = BLL.CustFeeInit.GetFeeInitByOrderId(orderId);
                if (feeInfo != null)
                {
                    el.Status = "SUC";
                    el.Result = feeInfo;
                    return com.loto.Common.SerializationHelper.GetSerializationString(el);
                }
                else
                {
                    return string.Empty;
                }
            }
            catch (Exception ex)
            {
                Log.Write(LogType.ERR, "GetFeeInitByOrderId(" + orderId + ") error:" + ex.Message);
                return string.Empty;
            }
        }

        //插入支付发起信息
        public string AddFeeInit(long initCustFid, byte initType, byte initFor, long initRelatedFid, string initChannel, decimal initChangeFee, decimal initPayFee, string initOutPayNo, string initOutPayDesc)
        {
            try
            {
                Response<OneResult> el = new Response<OneResult>();
                if (BLL.CustFeeInit.AddCustFeeInit(initCustFid, initType, initFor, initRelatedFid, initChannel, initChangeFee, initPayFee, initOutPayNo, initOutPayDesc))
                {
                    el.Status = "SUC";
                    el.Result = new OneResult("true");
                    return com.loto.Common.SerializationHelper.GetSerializationString(el);
                }
                else
                {
                    el.Status = "FAIL";
                    el.Result = new OneResult("false");
                    return com.loto.Common.SerializationHelper.GetSerializationString(el);
                }
            }
            catch (Exception ex)
            {
                Log.Write(LogType.ERR, "AddFeeInit(" + initCustFid + "," + initType + "," + initFor + "," + initRelatedFid + "," + initChannel + "," + initChangeFee + "," + initPayFee + "," + initOutPayNo + "," + initOutPayDesc + ") error:" + ex.Message);
                return string.Empty;
            }
        }

        //插入支付宝返回信息
        public string AddAlipayReturn(string returnIsSuc, string returnBuyerEmail, string returnBuyerId, string returnSellerEmail, string returnSellerId, string returnOutPayNo, decimal returnAmount, DateTime returnCreateTime, DateTime returnPayTime, string returnPayNo, string returnTradeStatus, string returnParams, decimal returnRefundFee, string returnRefundId, DateTime returnRefundTime, string returnRefundStatus)
        {
            try
            {
                Response<OneResult> el = new Response<OneResult>();
                if (BLL.AlipayReturn.InsertAlipayReturn(returnIsSuc, returnBuyerEmail, returnBuyerId, returnSellerEmail, returnSellerId, returnOutPayNo, returnAmount, returnCreateTime, returnPayTime, returnPayNo, returnTradeStatus, returnParams, returnRefundFee, returnRefundId, returnRefundTime, returnRefundStatus))
                {
                    el.Status = "SUC";
                    el.Result = new OneResult("true");
                    return com.loto.Common.SerializationHelper.GetSerializationString(el);
                }
                else
                {
                    el.Status = "FAIL";
                    el.Result = new OneResult("false");
                    return com.loto.Common.SerializationHelper.GetSerializationString(el);
                }
            }
            catch (Exception ex)
            {
                Log.Write(LogType.ERR, "AddAlipayReturn(" + returnIsSuc + "," + returnBuyerEmail + "," + returnBuyerId + "," + returnSellerEmail + "," + returnSellerId + "," + returnOutPayNo + "," + returnAmount + "," + returnCreateTime + "," + returnPayTime + "," + returnPayNo + "," + returnTradeStatus + "," + returnParams + "," + returnParams + "," + returnRefundFee + "," + returnRefundId + "," + returnRefundStatus + "," + returnRefundStatus + ") error:" + ex.Message);
                return string.Empty;
            }
        }

        //支付成功
        public string PaySuc(string orderId, string tradeNo, decimal fee, string channel, DateTime payTime)
        {
            try
            {
                Response<OneResult> el = new Response<OneResult>();
                bool isSuc = BLL.Pay.DoPay(orderId, tradeNo, fee, channel, payTime);
                if (isSuc)
                {
                    el.Status = "SUC";
                    el.Result = new OneResult("true");
                    return com.loto.Common.SerializationHelper.GetSerializationString(el);
                }
                else
                {
                    el.Status = "FAIL";
                    el.Result = new OneResult("false");
                    return com.loto.Common.SerializationHelper.GetSerializationString(el);
                }
            }
            catch (Exception ex)
            {
                Log.Write(LogType.ERR, "PaySuc(" + orderId + "," + tradeNo + "," + fee + "," + channel + "," + payTime + ") error:" + ex.Message);
                return string.Empty;
            }
        }

        //余额支付
        public string BalancePay(string orderId, long oId, decimal fee)
        {
            try
            {
                Response<OneResult> el = new Response<OneResult>();
                bool isSuc = BLL.Pay.DoBalancePay(orderId, oId, fee);
                if (isSuc)
                {
                    el.Status = "SUC";
                    el.Result = new OneResult("true");
                    return com.loto.Common.SerializationHelper.GetSerializationString(el);
                }
                else
                {
                    el.Status = "FAIL";
                    el.Result = new OneResult("false");
                    return com.loto.Common.SerializationHelper.GetSerializationString(el);
                }
            }
            catch (Exception ex)
            {
                Log.Write(LogType.ERR, "BalancePay(" + orderId + "," + oId + "," + fee + "," + fee + ") error:" + ex.Message);
                return string.Empty;
            }
        }

        //取消保单
        public string CancelPolicy(long policyId)
        {
            try
            {
                Response<OneResult> el = new Response<OneResult>();
                bool isSuc = BLL.Pay.CancelPolicy(policyId);
                if (isSuc)
                {
                    el.Status = "SUC";
                    el.Result = new OneResult("true");
                    return com.loto.Common.SerializationHelper.GetSerializationString(el);
                }
                else
                {
                    el.Status = "FAIL";
                    el.Result = new OneResult("false");
                    return com.loto.Common.SerializationHelper.GetSerializationString(el);
                }
            }
            catch (Exception ex)
            {
                Log.Write(LogType.ERR, "CancelPolicy(" + policyId + ") error:" + ex.Message);
                return string.Empty;
            }
        }
    }
}
